//
// Created by liaoy on 2021/8/19.
//

#ifndef _EM_VM_H
#define _EM_VM_H

#include <stdbool.h>
#include <stdint.h>
#include "em-vm-stub.h"

#define REGISTER_COUNT 10

typedef struct reg
{
    union
    {
        uint32_t integer;
        uint8_t *string;
        const uint8_t *pointer;
    } content;
    enum
    {
        INTEGER = 0,
        STRING,
        POINTER,
    } type;
} reg_t;

typedef struct
{
    reg_t registers[REGISTER_COUNT];

    uint8_t *data;
    const uint8_t *code;
    uint32_t ip;
    uint8_t *stack;
    uint32_t sp;

    reg_t (*syscall)(uint16_t id, uint8_t *params);

    uint8_t *bytecode;
    bool z_flag;
    bool running;
} em_vm_t;

em_vm_t *create_vm(uint8_t *bytecode, reg_t (*syscall)(uint16_t id, uint8_t *params));

bool vm_restart(em_vm_t *vm);

void vm_exec(em_vm_t *vm);

void vm_destroy(em_vm_t *vm);

#endif //_EM_VM_H
